package org.example.project.search.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.example.project.search.data.Suggestion

/**
 * 历史记录卡片
 */
@Composable
fun HistoryRecordCard(records: MutableState<List<Suggestion>>) {

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(top = 8.dp)
            .background(
                color = Color.White, shape = RoundedCornerShape(16.dp)
            ) // 设置背景色和圆角形状
            .padding(start = 12.dp, top = 8.dp, end = 12.dp, bottom = 8.dp),
    ) {
        // 顶部tip
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceAround
        ) {
            Tip("设置家", { println("我要回家") })
            Tip("去公司", { println("我要去公司") })
            Tip("收藏夹", { println("我要打开收藏夹") })
        }

        if (records.value.isNotEmpty()) {
            Divider(
                modifier = Modifier.padding(top = 8.dp, bottom = 8.dp),
                thickness = 0.5.dp
            )

            var isShowMore by remember { mutableStateOf(false) }

            Column {
                records.value.forEachIndexed { index, item ->
                    if (index >= 20 && !isShowMore) {
                        return@forEachIndexed
                    }
                    RecordItem(item)
                    Divider(
                        modifier = Modifier.padding(top = 4.dp, bottom = 4.dp),
                        thickness = 0.5.dp
                    )
                }
            }

            Row(
                modifier = Modifier.fillMaxWidth()
                    .padding(top = 6.dp, bottom = 3.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                if (records.value.size > 20 && !isShowMore) {
                    Text(
                        "更多历史记录", fontSize = 12.sp,
                        fontWeight = FontWeight.W400,
                        color = Color(0xcc000000),
                        modifier = Modifier.padding(end = 3.dp, start = 10.dp)
                            .clickable { isShowMore = true },
                    )
                    Img(10.dp)
                }
                Spacer(Modifier.weight(1f))
                Img(12.dp)
                Spacer(Modifier.width(6.dp))
                Text(
                    "清空",
                    fontSize = 12.sp,
                    fontWeight = FontWeight.W400,
                    modifier = Modifier.padding(end = 5.dp)
                        .clickable { records.value = emptyList() }
                )
            }
        }
    }
}

/**
 * 记录区域标签
 */
@Composable
fun Tip(title: String, action: () -> Unit = {}) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center,
        modifier = Modifier
            .clickable { action() }
    ) {
        Img(12.dp)
        Text(
            title,
            fontSize = 13.sp,
            fontWeight = FontWeight.W400,
            modifier = Modifier.padding(start = 4.dp)
        )
    }
}